package com.example.forum_system.service.impl;

import com.example.forum_system.common.AppVar;
import com.example.forum_system.common.Result;
import com.example.forum_system.common.ResultCode;
import com.example.forum_system.dao.ArticleMapper;
import com.example.forum_system.dao.UserMapper;
import com.example.forum_system.exception.AppException;
import com.example.forum_system.model.User;
import com.example.forum_system.model.VO.UserVO;
import com.example.forum_system.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @BelongsProject: 0.forum_system
 * @BelongsPackage: com.example.forum_system.service
 * @Author : 游在海里的鱼
 * @CreateTime : 2023-11-07  14:24
 * @Version : 1.0
 * @Description : TODO
 */
@Slf4j
@Service
@Component
public class UserService implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    ArticleMapper articleMapper;

    @Override
    public UserVO selectByUsername(String username) {
        UserVO user = userMapper.selectOneByUsername(username);
        if(user == null){
            log.error(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
            throw new AppException(Result.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        addArticleCount(user);
        return user;
    }

    @Override
    public UserVO selectById(long id) {
        UserVO user = userMapper.selectOneById(id);
        if(user == null){
            log.error("根据用户id查找用户失败");
            throw new AppException(Result.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        addArticleCount(user);
        return user;
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int insertUser(User user) {
        if(userMapper.selectOneByUsername(user.getUsername())!= null){
            log.error(ResultCode.FAILED_USER_EXISTS.getMessage());
            throw new AppException(Result.failed(ResultCode.FAILED_USER_EXISTS));
        }
        initialize(user);
        return userMapper.insertUser(user);
    }

    private void addArticleCount(UserVO user){
        int articleCount = articleMapper.selectCountByUser(user.getId());
        user.setArticleCount(articleCount);
    }

    private void initialize(User user){
        if(user.getUsername() == null){
            log.error(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
            throw new AppException(Result.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        if(user.getPassword() == null){
            user.setPassword("无");
        }
        if(user.getNickname() == null){
            user.setNickname("未命名");
        }
        if(user.getPhoneNum() == null){
            user.setPhoneNum("无");
        }
        if(user.getEmail() == null){
            user.setEmail("无");
        }
        if(user.getGender() == null){
            user.setGender(Byte.valueOf("0"));
        }
        if(user.getAvatarUrl() == null){
            user.setAvatarUrl(AppVar.AVATAR_URL);
        }
        if(user.getIsAdmin() == null){
            user.setIsAdmin(Byte.valueOf("0"));
        }
        if(user.getRemark() == null){
            user.setRemark("这是一个神秘的人，什么介绍都没有");
        }
        if(user.getState() == null){
            user.setState(Byte.valueOf("0"));
        }
        if(user.getDeleteState() == null){
            user.setDeleteState(Byte.valueOf("0"));
        }
        if(user.getCreateTime() == null){
            user.setCreateTime(new Date());
        }
        if(user.getUpdateTime() == null){
            user.setUpdateTime(new Date());
        }
    }
}
